const link = {
  async GetList(data) {
    let list = [];
    let total = 0;

    let query = "SELECT * FROM friend_link";
    let conditions = [];
    let params = [];

    if (data.keyword !== undefined && data.keyword !== "") {
      conditions.push("name LIKE ?");
      params.push(`%${data.keyword}%`);
    }

    if (conditions.length > 0) {
      query += " WHERE " + conditions.join(" AND ");
    }

    const [rows] = await global.mysqlPool.execute(query, params);
    list = rows;

    total = list.length;

    query += " ORDER BY id DESC LIMIT ? OFFSET ?";
    params.push(data.page_size, data.page_size * (data.page_num - 1));

    const [result] = await global.mysqlPool.execute(query, params);

    return { pageData: result, total };
  },
};

module.exports = link;
